<?php


namespace Tbf\Component\Arrays;


class TitleArrayOnGridArrayReader implements ArrayReaderInterface{
    /** @var \Tbf\Component\Arrays\ArrayReaderInterface  */
    protected $grid_array_reader;
    protected $line_num;
    protected $buffer_row = null;
    function __construct(ArrayReaderInterface $grid_array_reader){
        $this->grid_array_reader = $grid_array_reader;
        $this->is_first_line = true;
    }

    /**
     * 读出一个一维array数组
     * 如果返回值为null表示没有数据了
     * @return array data 数据
     */
    public function readRow()
    {
        if ($this->buffer_row!==null){
            $buffer_row = $this->buffer_row;
            $this->buffer_row = null;
            return $buffer_row;
        }
        $grid_row = $this->grid_array_reader->readRow();
        if ($grid_row===null){
            return null;
        }
        if ($this->is_first_line===true){
            $output = array_keys($grid_row);
            $this->buffer_row = array_values($grid_row);
            $this->is_first_line = false;
            return $output;
        }
        return array_values($grid_row);
    }

}